const axios = require('axios');

// 测试配置
const BASE_URL = 'http://localhost:8001';
const TEST_USER_ID = 'TEST_USER_001';
const TEST_BILL_NO = '7878611'; // 套餐促销单号

// 模拟用户token (前端接口需要)
const mockUserToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IlRFU1RfVVNFUl8wMDEiLCJpYXQiOjE2MzQ1Njc4OTB9.test';

// 测试套餐订单创建 - 使用前端接口
async function testCreateComboOrder() {
  try {
    console.log('=== 测试创建套餐订单 (前端接口) ===');
    
    const response = await axios.post(`${BASE_URL}/app/goods/promo/createComboOrder`, {
      billNo: TEST_BILL_NO,
      remark: '测试套餐订单'
    }, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${mockUserToken}`
      }
    });

    console.log('套餐订单创建成功:', response.data);
    return response.data;
  } catch (error) {
    console.error('套餐订单创建失败:', error.response?.data || error.message);
    return null;
  }
}

// 测试秒杀订单创建 - 使用前端接口
async function testCreateSeckillOrder() {
  try {
    console.log('\n=== 测试创建秒杀订单 (前端接口) ===');
    
    const response = await axios.post(`${BASE_URL}/app/goods/promo/createSeckillOrder`, {
      billNo: 'SECKILL_001', // 假设的秒杀单号
      goodsId: 'G4K7LVZ5WEJ',
      quantity: 1,
      remark: '测试秒杀订单'
    }, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${mockUserToken}`
      }
    });

    console.log('秒杀订单创建成功:', response.data);
    return response.data;
  } catch (error) {
    console.error('秒杀订单创建失败:', error.response?.data || error.message);
    return null;
  }
}

// 测试买赠订单创建 - 使用前端接口
async function testCreateBuyGiftOrder() {
  try {
    console.log('\n=== 测试创建买赠订单 (前端接口) ===');
    
    const response = await axios.post(`${BASE_URL}/app/goods/promo/createBuyGiftOrder`, {
      billNo: 'BUYGIFT_001', // 假设的买赠单号
      goodsId: 'G4K7LVZ5WEJ',
      quantity: 1,
      remark: '测试买赠订单'
    }, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${mockUserToken}`
      }
    });

    console.log('买赠订单创建成功:', response.data);
    return response.data;
  } catch (error) {
    console.error('买赠订单创建失败:', error.response?.data || error.message);
    return null;
  }
}

// 测试获取套餐详情
async function testGetComboDetails() {
  try {
    console.log('\n=== 测试获取套餐详情 ===');
    
    const response = await axios.post(`${BASE_URL}/app/goods/promoMt/getComboDetailsByType`, {
      promType: 'TC'
    }, {
      headers: {
        'Content-Type': 'application/json'
      }
    });

    console.log('套餐详情获取成功:', JSON.stringify(response.data, null, 2));
    return response.data;
  } catch (error) {
    console.error('套餐详情获取失败:', error.response?.data || error.message);
    return null;
  }
}

// 主测试函数
async function runTests() {
  console.log('开始测试促销订单API...\n');
  
  // 先测试获取套餐详情
  const comboDetails = await testGetComboDetails();
  
  // 等待一秒
  await new Promise(resolve => setTimeout(resolve, 1000));
  
  // 测试套餐订单
  await testCreateComboOrder();
  
  // 等待一秒
  await new Promise(resolve => setTimeout(resolve, 1000));
  
  // 测试秒杀订单
  await testCreateSeckillOrder();
  
  // 等待一秒
  await new Promise(resolve => setTimeout(resolve, 1000));
  
  // 测试买赠订单
  await testCreateBuyGiftOrder();
  
  console.log('\n测试完成!');
}

// 运行测试
runTests().catch(console.error); 